package com.example.com.example.imooc.AsynTask;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.examplee.username.httpfrist_01.R;

import java.util.List;

/**
 * Created by usernamee on 2017/7/28.
 */

public class NewsAdapter extends BaseAdapter implements AbsListView.OnScrollListener{
  private List<NewBean> mList;
    private LayoutInflater minflater;
    imageLoader mImageLoader;
    int mStart,mEnd;
     static String[] URLS;
    private  boolean mFristIn;
    public NewsAdapter(Context context, List<NewBean> data, ListView mListview){
        mList=data;
        minflater=LayoutInflater.from(context);
        mImageLoader=new imageLoader(mListview);
        URLS=new String[data.size()];
        for (int i=0;i<data.size();i++){
            URLS[i]=data.get(i).newsIconUrl;
        }
        mFristIn=true;
//        一定要注册
        mListview.setOnScrollListener(this);
    }
    @Override
    public int getCount() {
        return mList.size();
    }


    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder=null;
        if (convertView==null){
            viewHolder=new ViewHolder();
            convertView=minflater.inflate(R.layout.item_layout,null);
            viewHolder.ivIcon= (ImageView) convertView.findViewById(R.id.iv_icon);
            viewHolder.tvTitle= (TextView) convertView.findViewById(R.id.tv_title);
            viewHolder.tvContent= (TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
      viewHolder.ivIcon.setImageResource(R.drawable.imgasyn);
        String url=mList.get(position).newsIconUrl;
        viewHolder.ivIcon.setTag(url);
//        new imageLoader().showImageByThread(viewHolder.ivIcon,url);
//        new imageLoader().showImagesAsynTask(viewHolder.ivIcon,url);
        mImageLoader.showImagesAsynTask(viewHolder.ivIcon,url);//这样不会重复的调用imageLoader()
        viewHolder.tvTitle.setText(mList.get(position).newsTitle);
        viewHolder.tvContent.setText(mList.get(position).newsContent);
        return convertView;
    }

//onScrollStateChanged并不是每一次都会被调用
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
//        如果处于停止状态
        if (scrollState==SCROLL_STATE_IDLE){
            mImageLoader.loadImages(mStart,mEnd);
//            加载可见项
        }else{
            mImageLoader.cancelAllTask();
//            停止任务
        }
//
    }

//onScroll（）方法每次都会被调用
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//        firstVisibleItem第一个可见元素，totalItemCount可见元素的长度
        mStart=firstVisibleItem;
        mEnd=firstVisibleItem+visibleItemCount;
//        第一次显示的时候去调用
if (mFristIn==true && visibleItemCount>0){
    mImageLoader.loadImages(mStart,mEnd);
    mFristIn=false;
}
    }

    class ViewHolder{
        TextView tvTitle;
        TextView tvContent;
        ImageView ivIcon;
    }


}
